from rest_framework import serializers
from goods.models import SKU

class CartAddSerializer(serializers.Serializer):
    #商品编号
    sku_id = serializers.IntegerField()
    count = serializers.IntegerField(
        min_value=1,
        max_value=5,
        error_messages={
            'min_value':'单个用户限购1-5件',
            'max_value':'单个用户限购1-5件',
        },

    )
    selected = serializers.BooleanField(default=True)

    def validate_sku_id(self,value):
        if SKU.objects.filter(pk=value).count() <= 0:
            raise serializers.ValidationError('无效的商品编号')
        return value
    #不需要创建create方法，此处的保存是cookie，此处还没有创建Response



class CartListSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField()
    selected = serializers.BooleanField()
    class Meta:
        model = SKU
        fields = ['id','name','count','selected','default_image_url','price']


class CartSelectionSerializer(serializers.Serializer):
    selected = serializers.BooleanField()
